package com.sky.proxy.cglibProxy;


import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author : wushikai
 * <p>
 * date : 2022-03-09
 */
public class Client {

    static {
        //DebuggingClassWriter.DEBUG_LOCATION_PROPERTY（也就是 cglib.debugLocation）系统属性设置为当前项目的根目录，即可保存 CGLIB 生成的代理类到当前项目根目录下。
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, System.getProperty("user.dir"));
    }


    public static void main(String[] args) {


        MethodInterceptor methodInterceptor = new ServiceInterceptor();

        TerminalServiceImpl terminalService = CglibProxyFactory.createProxy(TerminalServiceImpl.class, methodInterceptor);
        terminalService.findByTerminalName("华智融音箱 9228");
        terminalService.findByTerminalId(9228);

        MerchantServiceImpl merchantService = CglibProxyFactory.createProxy(MerchantServiceImpl.class, methodInterceptor);
        merchantService.findByMerchantId(222);
        merchantService.findByMerchantName("广西卡海网络");

    }

    public static void rawCglib() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(MerchantServiceImpl.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("代理对象剖析.....");
                return proxy.invokeSuper(obj, args);
            }
        });
        MerchantServiceImpl merchantServiceRaw = (MerchantServiceImpl) enhancer.create();
        merchantServiceRaw.findByMerchantId(222);
        merchantServiceRaw.findByMerchantName("广西卡海网络");

    }


}
